package someTestExcemple.tcp.bs_tcp;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Server {
    //B-S架构的tcp通信
    public static void main(String[] args) throws IOException {
        System.out.println("服务端启动成功~~~");
        ServerSocket serverSocket = new ServerSocket(8083);
        //创建一个线程池
        ThreadPoolExecutor poolExecutor =
       new ThreadPoolExecutor(16,32,1, TimeUnit.MILLISECONDS,
               new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory(),
               new ThreadPoolExecutor.AbortPolicy());
        while (true) {
            //等待浏览器端的连接请求
            /**
             * 三次连接提示是由以下原因导致的：
             *
             * 1.主 HTML 请求
             * 2.自动触发的 favicon.ico 请求
             * 3.浏览器预连接优化
             */
            Socket socket = serverSocket.accept();
            System.out.println("一浏览器连接了"+ socket.getRemoteSocketAddress());
            poolExecutor.execute(new ServerReaderRunnable(socket));
        }

    }
}
